﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Linq;
using System.Data.OracleClient;

public partial class Admin_dondathang : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        HienThiDanhSach();
    }

    void HienThiDanhSach()
    {
        DataTable dtdondathang = DataConnection.GetDatatableByStoreProcedureNameNoParam("sp_getAllDonHang");
        GridView1.DataSource = dtdondathang;
        GridView1.DataBind();

        MultiView1.ActiveViewIndex = 0;

    }

    protected void btntmkiem_Click(object sender, EventArgs e)
    {
        
        string tenusers = txttim.Text;
        OracleParameter[] thamso = new OracleParameter[]{
            new OracleParameter("p_content",tenusers)
        };

        // search DonHang by TenUsers
        DataTable dt = DataConnection.GetDatatableByStoreProcedureNameCursor("sp_SearchDonHangByTenUsers", thamso);
        GridView1.DataSource = dt;
        GridView1.DataBind();
        txttim.Text = "";
    }


    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string maddh = GridView1.SelectedRow.Cells[2].Text;
        HienThiKetQuaSua(maddh);

    }

    void HienThiKetQuaSua(string maddh)
    {
        
        OracleParameter[] thamso = new OracleParameter[]
        {
            new OracleParameter("p_madonhang",maddh)
        };

        // get DonHang By MaDonHang
        DataTable dtddh = DataConnection.GetDatatableByStoreProcedureNameCursor("sp_getDonHangByMaDonHang", thamso);

        lbmaddh.Text = dtddh.Rows[0][0].ToString();
        lbdiachigiao.Text = dtddh.Rows[0][4].ToString();
        lbtendn.Text = dtddh.Rows[0][1].ToString();
        lbngaydat.Text = dtddh.Rows[0][2].ToString();
        lbngaygiao.Text = dtddh.Rows[0][5].ToString();
        lbtennguoinhan.Text = dtddh.Rows[0][8].ToString();
        string t;
        if (dtddh.Rows[0][7].ToString() == "0")
        {
            t = "False";
        }
        else
            t = "True";
        DropDownList1.SelectedValue = t;
        lbtongtien.Text = dtddh.Rows[0][3].ToString() + " VNĐ";
        lbphivanchuyen.Text = dtddh.Rows[0][6].ToString() + " VNĐ";
        MultiView1.ActiveViewIndex = 1;
       
        OracleParameter[] thamso1 = new OracleParameter[]
        {
            new OracleParameter("p_madonhang",maddh),
        };
        DataTable dtcthd = DataConnection.GetDatatableByStoreProcedureNameCursor("sp_getDonChiTietHoaDon", thamso1);
        GridView2.DataSource = dtcthd;
        GridView2.DataBind();

    }

    protected void btnupdate_Click(object sender, EventArgs e)
    {
        int tinhtrang;
        string maddh = lbmaddh.Text;
        if (DropDownList1.SelectedValue == "True")
            tinhtrang = 1;
        else
            tinhtrang = 0;
         
       
        OracleParameter[] thamso = new OracleParameter[]
        {
            new OracleParameter("p_tinhtrang",tinhtrang),
            new OracleParameter("p_madonhang",maddh),
        };
        DataConnection.GetDatatableByStoreProcedureName("updateDonHangByMaDonHang", thamso);
        this.HienThiDanhSach();
    }



 



    protected void btnback_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 0;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        MultiView1.ActiveViewIndex = 0;
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string maddh = GridView1.Rows[e.RowIndex].Cells[2].Text;

        
        OracleParameter[] thamSo = new OracleParameter[]{
            new OracleParameter("p_madonhang",maddh)
        };

        // delete DonHang By MaDonHang
        DataConnection.GetDatatableByStoreProcedureName("deleteDonHangByMaDonHang", thamSo);
        HienThiDanhSach();

    }


    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        this.HienThiDanhSach();
    }
}